package com.example.myapplication;


import com.example.myapplication.audio.MyMusic;
import com.example.myapplication.audio.MySoundPool;
import com.example.myapplication.enemy.Cannon;
import com.example.myapplication.enemy.Chestunt;
import com.example.myapplication.enemy.Enemy;
import com.example.myapplication.enemy.Turtle;
import com.example.myapplication.enums.GameState;
import com.example.myapplication.enums.ItemType;
import com.example.myapplication.enums.Site;
import com.example.myapplication.item.*;
import com.example.myapplication.item.brick.Brick;
import com.example.myapplication.item.brick.Broken;
import com.example.myapplication.item.brick.CommonBrick;
import com.example.myapplication.item.brick.Pipe;
import com.example.myapplication.util.Identifier;
import ohos.accessibility.ability.GestureResultListener;
import ohos.agp.components.Component;
import ohos.agp.render.*;
import ohos.agp.render.render3d.ViewHolder;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.data.preferences.Preferences;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;
import ohos.global.configuration.DeviceCapability;
import ohos.global.resource.NotExistException;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;
import ohos.multimodalinput.event.MmiPoint;
import ohos.multimodalinput.event.TouchEvent;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import static com.example.myapplication.enums.GameState.FINISH;
import static com.example.myapplication.enums.GameState.GAMEOVER;
import static com.example.myapplication.enums.GameState.GAMING;
import static com.example.myapplication.enums.GameState.LIFTCOUNTER;
import static com.example.myapplication.enums.GameState.LOGO;
import static com.example.myapplication.enums.GameState.TRANSFER;
import static com.example.myapplication.enums.Site.上中;
import static com.example.myapplication.enums.Site.上右;
import static com.example.myapplication.enums.Site.上左;
import static com.example.myapplication.enums.Site.下;
import static com.example.myapplication.enums.Site.下中;
import static com.example.myapplication.enums.Site.下右;
import static com.example.myapplication.enums.Site.下左;
import static com.example.myapplication.enums.Site.右;
import static com.example.myapplication.enums.Site.右上;
import static com.example.myapplication.enums.Site.右下;
import static com.example.myapplication.enums.Site.右中;
import static com.example.myapplication.enums.Site.左;
import static com.example.myapplication.enums.Site.左上;
import static com.example.myapplication.enums.Site.左下;
import static com.example.myapplication.enums.Site.左中;

/**
 * 自定义游戏界面类：Created by Yaoyuanling on 2021/12/25.
 */

public class MySupperTiggerView extends Component implements Runnable ,Component.EstimateSizeListener{
    public static final String TAG = "aaa";
    private MyMusic myMusic;
    private MySoundPool mySoundPool;
    private Preferences sp;
    private boolean isPause;
    private Component marioComponent;
    private ViewHolder holder;
    private boolean isRunning;
    private Canvas lockCanvas;
    private float scaleX;
    private float scaleY;
    private Context context;
    private PixelMap aBitmap;
    private PixelMapHolder aPixelMapHolder;
    private PixelMapHolder bBitmap;
    private PixelMapHolder leftBitmap;
    private PixelMapHolder rightBitmap;
    private RectFloat rectF;
    private Mario mario;
    private TiledLayer tiledLayer_peng01;
    private PixelMapHolder gameoverbitmap;
    private List<Sprite> chestunts;
    private List<PixelMapHolder> chestuntBitmaps;
    private boolean isEnemyShown1;
    private boolean isEnemyShown2;
    private boolean isEnemyShown3;
    private Integer score;//用于记录分数
    private Paint mPaint;
    private PixelMapHolder marioBitmap;
    private int lifeNumber = 3;
    private boolean isInited;
    private String scoreString;
    private int delay;
    private PixelMapHolder logoBitmap;
    private List<Sprite> bricks;
    private int time;//表示剩余时间
    private Thread thread;
    private PixelMapHolder finishBitmap;
    private boolean threadRunning;
    private PixelMapHolder coinBitmap;
    private int coinNumber;
    private GameState gameState;//表示当前游戏状态
    private int stateDelay_lifecounter;
    private int stateDelay_finish;
    private int stateDelay_logo;
    private int stateDelay_gameover;
    private boolean isFirstRun;
    private boolean isMaioDieSoundPlayed;
    private List<Sprite> commonBricks;
    private TiledLayer tiledLayer_cover;
    private PixelMapHolder enemyBulletBitmap;
    private List<PixelMapHolder> cannonBitmaps;
    private List<PixelMapHolder> turtleBitmaps;
    private List<Sprite> cannons;
    private List<Sprite> turtles;
    private List<Sprite> items;
    private boolean isEnemyShown4;
    private boolean isEnemyShown5;
    private List<PixelMapHolder> fireBitmaps;
    private PixelMapHolder downBitmap;
    private List<Sprite> pipes;
    private boolean isTransferReady;
    private boolean isTransferDone;
    private EventHandler handler;

    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "SupperTigger");
    private int whirlCount = 0;
    private boolean isCorp = false;
    private boolean isScale = false;
    private boolean isMirror = false;

    /**
     *
     */
    private DrawTask task = new DrawTask() {
        @Override
        public void onDraw(Component component, Canvas canvas) {
            System.out.println("=================OnDraw===============");
            drawBg(canvas,mPaint);
            lockCanvas = canvas;
        }
    };

    public Component getMarioComponent() {
        return marioComponent;
    }

    public void setMarioComponent(Component marioComponent) {
        this.marioComponent = marioComponent;
    }

    public static String getTAG() {
        return TAG;
    }

    public MyMusic getMyMusic() {
        return myMusic;
    }

    public void setMyMusic(MyMusic myMusic) {
        this.myMusic = myMusic;
    }

    public MySoundPool getMySoundPool() {
        return mySoundPool;
    }

    public void setMySoundPool(MySoundPool mySoundPool) {
        this.mySoundPool = mySoundPool;
    }

    public Preferences getSp() {
        return sp;
    }

    public void setSp(Preferences sp) {
        this.sp = sp;
    }

    public ViewHolder getHolder() {
        return holder;
    }

    public void setHolder(ViewHolder holder) {
        this.holder = holder;
    }

    public boolean isRunning() {
        return isRunning;
    }

    public void setRunning(boolean running) {
        isRunning = running;
    }

    public Canvas getLockCanvas() {
        return lockCanvas;
    }

    public void setLockCanvas(Canvas lockCanvas) {
        this.lockCanvas = lockCanvas;
    }

    @Override
    public float getScaleX() {
        return scaleX;
    }

    @Override
    public void setScaleX(float scaleX) {
        this.scaleX = scaleX;
    }

    @Override
    public float getScaleY() {
        return scaleY;
    }

    @Override
    public void setScaleY(float scaleY) {
        this.scaleY = scaleY;
    }

    @Override
    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public PixelMap getaBitmap() {
        return aBitmap;
    }

    public void setaBitmap(PixelMap aBitmap) {
        this.aBitmap = aBitmap;
    }

    public PixelMapHolder getaPixelMapHolder() {
        return aPixelMapHolder;
    }

    public void setaPixelMapHolder(PixelMapHolder aPixelMapHolder) {
        this.aPixelMapHolder = aPixelMapHolder;
    }

    public PixelMapHolder getbBitmap() {
        return bBitmap;
    }

    public void setbBitmap(PixelMapHolder bBitmap) {
        this.bBitmap = bBitmap;
    }

    public PixelMapHolder getLeftBitmap() {
        return leftBitmap;
    }

    public void setLeftBitmap(PixelMapHolder leftBitmap) {
        this.leftBitmap = leftBitmap;
    }

    public PixelMapHolder getRightBitmap() {
        return rightBitmap;
    }

    public void setRightBitmap(PixelMapHolder rightBitmap) {
        this.rightBitmap = rightBitmap;
    }

    public RectFloat getRectF() {
        return rectF;
    }

    public void setRectF(RectFloat rectF) {
        this.rectF = rectF;
    }

    public Mario getMario() {
        return mario;
    }

    public void setMario(Mario mario) {
        this.mario = mario;
    }

    public TiledLayer getTiledLayer_peng01() {
        return tiledLayer_peng01;
    }

    public void setTiledLayer_peng01(TiledLayer tiledLayer_peng01) {
        this.tiledLayer_peng01 = tiledLayer_peng01;
    }

    public PixelMapHolder getGameoverbitmap() {
        return gameoverbitmap;
    }

    public void setGameoverbitmap(PixelMapHolder gameoverbitmap) {
        this.gameoverbitmap = gameoverbitmap;
    }

    public List<Sprite> getChestunts() {
        return chestunts;
    }

    public void setChestunts(List<Sprite> chestunts) {
        this.chestunts = chestunts;
    }

    public List<PixelMapHolder> getChestuntBitmaps() {
        return chestuntBitmaps;
    }

    public void setChestuntBitmaps(List<PixelMapHolder> chestuntBitmaps) {
        this.chestuntBitmaps = chestuntBitmaps;
    }

    public boolean isEnemyShown1() {
        return isEnemyShown1;
    }

    public void setEnemyShown1(boolean enemyShown1) {
        isEnemyShown1 = enemyShown1;
    }

    public boolean isEnemyShown2() {
        return isEnemyShown2;
    }

    public void setEnemyShown2(boolean enemyShown2) {
        isEnemyShown2 = enemyShown2;
    }

    public boolean isEnemyShown3() {
        return isEnemyShown3;
    }

    public void setEnemyShown3(boolean enemyShown3) {
        isEnemyShown3 = enemyShown3;
    }

    public Integer getScore() {
        return score;
    }

    public void setScore(Integer score) {
        this.score = score;
    }

    public Paint getmPaint() {
        return mPaint;
    }

    public void setmPaint(Paint mPaint) {
        this.mPaint = mPaint;
    }

    public PixelMapHolder getMarioBitmap() {
        return marioBitmap;
    }

    public void setMarioBitmap(PixelMapHolder marioBitmap) {
        this.marioBitmap = marioBitmap;
    }

    public int getLifeNumber() {
        return lifeNumber;
    }

    public void setLifeNumber(int lifeNumber) {
        this.lifeNumber = lifeNumber;
    }

    public boolean isInited() {
        return isInited;
    }

    public void setInited(boolean inited) {
        isInited = inited;
    }

    public String getScoreString() {
        return scoreString;
    }

    public void setScoreString(String scoreString) {
        this.scoreString = scoreString;
    }

    public int getDelay() {
        return delay;
    }

    public void setDelay(int delay) {
        this.delay = delay;
    }

    public PixelMapHolder getLogoBitmap() {
        return logoBitmap;
    }

    public void setLogoBitmap(PixelMapHolder logoBitmap) {
        this.logoBitmap = logoBitmap;
    }

    public List<Sprite> getBricks() {
        return bricks;
    }

    public void setBricks(List<Sprite> bricks) {
        this.bricks = bricks;
    }

    public int getTime() {
        return time;
    }

    public void setTime(int time) {
        this.time = time;
    }

    public Thread getThread() {
        return thread;
    }

    public void setThread(Thread thread) {
        this.thread = thread;
    }

    public PixelMapHolder getFinishBitmap() {
        return finishBitmap;
    }

    public void setFinishBitmap(PixelMapHolder finishBitmap) {
        this.finishBitmap = finishBitmap;
    }

    public boolean isThreadRunning() {
        return threadRunning;
    }

    public void setThreadRunning(boolean threadRunning) {
        this.threadRunning = threadRunning;
    }

    public PixelMapHolder getCoinBitmap() {
        return coinBitmap;
    }

    public void setCoinBitmap(PixelMapHolder coinBitmap) {
        this.coinBitmap = coinBitmap;
    }

    public int getCoinNumber() {
        return coinNumber;
    }

    public void setCoinNumber(int coinNumber) {
        this.coinNumber = coinNumber;
    }

    public GameState getGameState() {
        return gameState;
    }

    public void setGameState(GameState gameState) {
        this.gameState = gameState;
    }

    public int getStateDelay_lifecounter() {
        return stateDelay_lifecounter;
    }

    public void setStateDelay_lifecounter(int stateDelay_lifecounter) {
        this.stateDelay_lifecounter = stateDelay_lifecounter;
    }

    public int getStateDelay_finish() {
        return stateDelay_finish;
    }

    public void setStateDelay_finish(int stateDelay_finish) {
        this.stateDelay_finish = stateDelay_finish;
    }

    public int getStateDelay_logo() {
        return stateDelay_logo;
    }

    public void setStateDelay_logo(int stateDelay_logo) {
        this.stateDelay_logo = stateDelay_logo;
    }

    public int getStateDelay_gameover() {
        return stateDelay_gameover;
    }

    public void setStateDelay_gameover(int stateDelay_gameover) {
        this.stateDelay_gameover = stateDelay_gameover;
    }

    public boolean isFirstRun() {
        return isFirstRun;
    }

    public void setFirstRun(boolean firstRun) {
        isFirstRun = firstRun;
    }

    public boolean isMaioDieSoundPlayed() {
        return isMaioDieSoundPlayed;
    }

    public void setMaioDieSoundPlayed(boolean maioDieSoundPlayed) {
        isMaioDieSoundPlayed = maioDieSoundPlayed;
    }

    public List<Sprite> getCommonBricks() {
        return commonBricks;
    }

    public void setCommonBricks(List<Sprite> commonBricks) {
        this.commonBricks = commonBricks;
    }

    public TiledLayer getTiledLayer_cover() {
        return tiledLayer_cover;
    }

    public void setTiledLayer_cover(TiledLayer tiledLayer_cover) {
        this.tiledLayer_cover = tiledLayer_cover;
    }

    public PixelMapHolder getEnemyBulletBitmap() {
        return enemyBulletBitmap;
    }

    public void setEnemyBulletBitmap(PixelMapHolder enemyBulletBitmap) {
        this.enemyBulletBitmap = enemyBulletBitmap;
    }

    public List<PixelMapHolder> getCannonBitmaps() {
        return cannonBitmaps;
    }

    public void setCannonBitmaps(List<PixelMapHolder> cannonBitmaps) {
        this.cannonBitmaps = cannonBitmaps;
    }

    public List<PixelMapHolder> getTurtleBitmaps() {
        return turtleBitmaps;
    }

    public void setTurtleBitmaps(List<PixelMapHolder> turtleBitmaps) {
        this.turtleBitmaps = turtleBitmaps;
    }

    public List<Sprite> getCannons() {
        return cannons;
    }

    public void setCannons(List<Sprite> cannons) {
        this.cannons = cannons;
    }

    public List<Sprite> getTurtles() {
        return turtles;
    }

    public void setTurtles(List<Sprite> turtles) {
        this.turtles = turtles;
    }

    public List<Sprite> getItems() {
        return items;
    }

    public void setItems(List<Sprite> items) {
        this.items = items;
    }

    public boolean isEnemyShown4() {
        return isEnemyShown4;
    }

    public void setEnemyShown4(boolean enemyShown4) {
        isEnemyShown4 = enemyShown4;
    }

    public boolean isEnemyShown5() {
        return isEnemyShown5;
    }

    public void setEnemyShown5(boolean enemyShown5) {
        isEnemyShown5 = enemyShown5;
    }

    public List<PixelMapHolder> getFireBitmaps() {
        return fireBitmaps;
    }

    public void setFireBitmaps(List<PixelMapHolder> fireBitmaps) {
        this.fireBitmaps = fireBitmaps;
    }

    public PixelMapHolder getDownBitmap() {
        return downBitmap;
    }

    public void setDownBitmap(PixelMapHolder downBitmap) {
        this.downBitmap = downBitmap;
    }

    public List<Sprite> getPipes() {
        return pipes;
    }

    public void setPipes(List<Sprite> pipes) {
        this.pipes = pipes;
    }

    public boolean isTransferReady() {
        return isTransferReady;
    }

    public void setTransferReady(boolean transferReady) {
        isTransferReady = transferReady;
    }

    public boolean isTransferDone() {
        return isTransferDone;
    }

    public void setTransferDone(boolean transferDone) {
        isTransferDone = transferDone;
    }

    public boolean isPause() {
        return isPause;
    }

    public void setPause(boolean pause) {
        isPause = pause;
    }



    public MySupperTiggerView(Context context) {
        super(context);
        this.sp = sp;
        this.context = context;
        //获取屏幕尺寸以便进行缩放调整
        DeviceCapability deviceCapability = context.getResourceManager().getDeviceCapability();
        //获取对应在缩放比例
        scaleX = (float) deviceCapability.width/ 800;
        scaleY = (float) deviceCapability.height / 480;
        myMusic = new MyMusic(context);
        mySoundPool = new MySoundPool(context);
        mPaint = new Paint();
        isRunning = true;
        EventRunner runner1 = EventRunner.current(); // 获取主线程
        handler = new EventHandler(runner1);
        addDrawTask(task);
        new Thread(this).start();
        this.setTouchEventListener(new Component.TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                HiLog.info(LABEL_LOG, "onTouchEvent: 滑动监听器" + touchEvent.getAction());
                if (gameState != null) {
                    switch (gameState) {
                        case LOGO: {
                            System.out.println("=====================================" + stateDelay_logo);
                            if (stateDelay_logo > 40) {
                                init();
                                stateDelay_logo = 0;
                                coinNumber = 0;
                            }
                        }
                        break;
                        case FINISH: {
                            System.out.println("=====================================" + stateDelay_finish);
                            if (stateDelay_finish > 100) {
                                gameState = LOGO;
                                lifeNumber = 3;
                                stateDelay_finish = 0;
                            }
                        }
                        break;
                        case GAMEOVER: {
                            System.out.println("=====================================" + stateDelay_gameover);
                            if (stateDelay_gameover > 40) {
                                gameState = LOGO;
                                lifeNumber = 3;
                                stateDelay_gameover = 0;
                            }

                        }
                        case GAMING: {
                            MmiPoint absPoint = touchEvent.getPointerPosition(touchEvent.getIndex());
                            int pointCount = touchEvent.getPointerCount();
                            for (int i = 0; i < pointCount; i++) {
                                rectF = new RectFloat(0, 360 * scaleY, 60 * scaleX, 420 * scaleY);
                                if (rectF.isInclude(touchEvent.getPointerScreenPosition(i).getX(), touchEvent.getPointerScreenPosition(i).getY())) {
                                    //left running
                                    mario.setMirror(true);
                                    mario.setRunning(true);
                                }
                                rectF = new RectFloat(60, 420 * scaleY, 120 * scaleX, 480 * scaleY);
                                if (rectF.isInclude(touchEvent.getPointerScreenPosition(i).getX(), touchEvent.getPointerScreenPosition(i).getY())) {
                                    if (isTransferReady) {
                                        gameState = TRANSFER;
//                                mySoundPool.play(mySoundPool.getTransferSound());
                                    }
                                }

//                        rectF.set(120, 360 * scaleY, 180 * scaleX, 420 * scaleY);
                                rectF = new RectFloat(120, 360 * scaleY, 180 * scaleX, 420 * scaleY);
                                if (rectF.isInclude(touchEvent.getPointerScreenPosition(i).getX(), touchEvent.getPointerScreenPosition(i).getY())) {
                                    //right running
                                    mario.setMirror(false);
                                    mario.setRunning(true);
                                }
                                rectF = new RectFloat(740, 360 * scaleY, 800 * scaleX, 420 * scaleY);
                                if (rectF.isInclude(touchEvent.getPointerScreenPosition(i).getX(), touchEvent.getPointerScreenPosition(i).getY())) {
                                    //jump
                                    if (!mario.isDead() && !mario.isJumping()) {
                                        mario.setJumping(true);
//                                mySoundPool.play(mySoundPool.getJumpSound());
                                        mario.setSpeedY(-16);
                                    }
                                }

                                rectF= new RectFloat(680, 420 * scaleY, 740 * scaleX, 4800 * scaleY);
                                if (rectF.isInclude(touchEvent.getPointerScreenPosition(i).getX(), touchEvent.getPointerScreenPosition(i).getY())) {
                                    mario.fire();
                                }
                                //手指移开时停止移动
                                if (touchEvent.getAction() == TouchEvent.HOVER_POINTER_EXIT) {
                                    //stop running
                                    mario.setRunning(false);
//                            performClick();
                                }
                            }
                        }
                        break;
                    }

                }
                return true;
            }
        });
    }


    /**
     * 通过图片ID返回PixelMap
     *
     * @param resourceId 图片的资源ID
     * @return 图片的PixelMap
     */
    private PixelMap getPixelMapFromResource(int resourceId) {
        InputStream inputStream = null;
        try {
            // 创建图像数据源ImageSource对象
            inputStream = getContext().getResourceManager().getResource(resourceId);
            ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions();
            srcOpts.formatHint = "image/jpg";
            ImageSource imageSource = ImageSource.create(inputStream, srcOpts);

            // 设置图片参数
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            // 旋转
            decodingOptions.rotateDegrees = 90 * whirlCount;
            // 缩放
            decodingOptions.desiredSize = new Size(isScale ? 512 : 0, isScale ? 384 : 0);
            // 剪裁
            decodingOptions.desiredRegion = new Rect(0, 0, isCorp ? 1024 : 0, isCorp ? 400 : 0);
            decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
            return imageSource.createPixelmap(decodingOptions);
        } catch (IOException e) {
            HiLog.info(LABEL_LOG, "IOException");
        } catch (NotExistException e) {
            HiLog.info(LABEL_LOG, "NotExistException");
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    HiLog.info(LABEL_LOG, "inputStream IOException");
                }
            }
        }
        return null;
    }
    //画背景 这里通过sXOffset, sYOffset可以实现对蛇活动区域的限制
    private void drawBg(Canvas canvas, Paint paint) {
        paint.setAntiAlias(true);
        paint.setStrokeWidth(60);
        paint.setStrokeCap(Paint.StrokeCap.ROUND_CAP);
        paint.setStyle(Paint.Style.STROKE_STYLE);
        //获取PixeMap对象
        PixelMap pixelMap = getPixelMapFromResource(ResourceTable.Media_big_mario_00);
        //获取PixelMapHolder对象
        RectFloat centerRectFloat = new RectFloat();
        PixelMapHolder pixelMapHolder = new PixelMapHolder(pixelMap);
        canvas.drawPixelMapHolder(pixelMapHolder,0,0,paint);
    }


    /**
     * 2022/1/9  performClick()方法的编写
     */
    private void performClick() {

    }


    /**
     * 加载图片
     *
     * @param fileId 图片文件ID
     * @return Bitmap
     */
    public PixelMapHolder getBitmap(int fileId) {
        PixelMap pixelMap = getPixelMapFromResource(fileId);
        //获取PixelMapHolder对象
        PixelMapHolder bitmap = new PixelMapHolder(pixelMap);
        return bitmap;
    }

    /**
     * 加载图片
     *
     * @param fileName 图片文件名称
     * @return Bitmap
     */
    public PixelMapHolder getPixelMapHolderByFileName(String fileName) {
        //以文件ID获取输入流
        Identifier identifier = new Identifier();
        //获取文件ID
        int media = identifier.getIdentifier(mContext, "Media", fileName);
        PixelMap pixelMap = getPixelMapFromResource(media);
        //获取PixelMapHolder对象
        PixelMapHolder pixelMapHolder  = new PixelMapHolder(pixelMap);
        return pixelMapHolder;
    }

    private int[][] getMapArray() {
        return new int[][]{
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 27, 27, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 27, 27, 27, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 27, 27, 27, 27, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 27, 27, 27, 27, 27, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 27, 0, 0, 0, 0, 0, 0, 0,
                        0, 27, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 27, 27, 27, 27, 27, 27, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 27, 27, 0, 0, 0, 0, 0, 0, 0,
                        0, 27, 27, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 27, 27, 27, 27, 27, 27, 27, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 27, 27, 27, 0, 0, 0, 0, 0, 0, 0,
                        0, 27, 27, 27, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 27, 27, 27, 27, 27, 27, 27, 27, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0},
                {26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
                        26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
                        26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
                        26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 0, 0, 26, 26, 26, 26,
                        26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
                        26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
                        26, 26, 26, 26},
                {30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
                        30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
                        30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
                        30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 0, 0, 30, 30, 30, 30,
                        30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
                        30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
                        30, 30, 30, 30},
                {30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
                        30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
                        30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
                        30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 0, 0, 30, 30, 30, 30,
                        30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
                        30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
                        30, 30, 30, 30}
        };
    }

    private int[][] getMapCoverArray() {
        return new int[][]{
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 50, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0},
                {0, 0, 0, 55, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 45, 50, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 45, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        55, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 55, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41,
                        42, 43, 44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 46,
                        47, 48, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 18, 19,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 12, 13, 14, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 51,
                        52, 53, 54, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0}
        };
    }

    /**
     * 通用的精灵移动方法
     *
     * @param sprites   目标精灵
     * @param distenceX x轴移动距离
     */
    private void spritesMove(List<Sprite> sprites, int distenceX) {
        if (sprites != null) {
            for (int i = 0; i < sprites.size(); i++) {
                Sprite sprite = sprites.get(i);
                sprite.move(distenceX, 0);
                if (sprite instanceof CommonBrick) {
                    Broken broken = ((CommonBrick) sprite).getBroken();
                    if (broken != null && broken.isVisiable()) {
                        broken.move(distenceX, 0);
                    }
                }
            }
        }
    }

    /**
     * 通用精灵绘制方法
     *
     * @param sprites 目标精灵
     * @param canvas  画布对象
     */
    private void spritesDraw(List<Sprite> sprites, Canvas canvas) {
        if (sprites != null) {
            System.out.println("===================spritesDraw===================" + sprites.size());
            for (int i = 0; i < sprites.size(); i++) {
                Sprite sprite = sprites.get(i);
                sprite.draw(canvas);
                //绘制普通砖块破碎效果
                if (sprite instanceof CommonBrick) {
                    Broken broken = ((CommonBrick) sprite).getBroken();
                    if (broken != null && broken.isVisiable()) {
                        broken.draw(canvas);
                    }
                }
            }
        }
    }

    private void spritesLogic(List<Sprite> sprites) {
        if (sprites != null) {
            for (int i = 0; i < sprites.size(); i++) {
                Sprite sprite = sprites.get(i);
                sprite.logic();
                //普通砖块破碎效果逻辑
                if (sprite instanceof CommonBrick) {
                    Broken broken = ((CommonBrick) sprite).getBroken();
                    if (broken != null && broken.isVisiable()) {
                        broken.logic();
                    }
                }
            }

        }
    }


    //endregion
    private void myLogic() {
        switch (gameState) {
            case TRANSFER: {
                if (!mario.isDead()) {
                    mario.move(0, 3);
                    if (mario.getY() > 280) {
                        tiledLayer_peng01.move(-980, 0);
                        tiledLayer_cover.move(-980, 0);
                        //到达地图边界 人物移动
                        spritesMove(chestunts, -980);
                        spritesMove(cannons, -980);
                        spritesMove(turtles, -980);
                        spritesMove(bricks, -980);
                        spritesMove(commonBricks, -980);
                        spritesMove(items, -980);
                        spritesMove(pipes, -980);
                        isTransferDone = false;
                        gameState = GAMING;
                    }
                }
            }
            break;
            case GAMING: {
                if (!threadRunning && isInited) {
                    thread.start();
                    threadRunning = true;
                }
                if(!isTransferDone && mario.getY()>216){
                    mario.move(0,-3);
                    if(mario.getY()<=216){
                        isTransferDone = true;
                    }
                }else{
                    mario.logic();
                    if (!mario.isDead()) {

                        if (mario.isInvincible()) {
//                            myMusic.play(ResourceTable.Media_invincible, true);
                        } else {
//                            myMusic.play(ResourceTable.Media_bgm, true);
                        }
                        spritesLogic(cannons);
                        spritesLogic(chestunts);
                        spritesLogic(mario.getBullets());
                        spritesLogic(commonBricks);
                        spritesLogic(items);
                        spritesLogic(turtles);
                        spritesLogic(bricks);
                        spritesLogic(pipes);
                    } else {
                        if (!isMaioDieSoundPlayed) {
//                            myMusic.play(ResourceTable.Media_over, false);
                            isMaioDieSoundPlayed = true;
                        }
                    }
                    marioMove();
                    myStep();
                    collisionWithMap();
                    myCollision();
                }

                myTime();
            }
            break;
            case LIFTCOUNTER: {
                myMusic.stop();
                if (lifeNumber < 1) {
                    gameState = GAMEOVER;
                } else {
                    if (stateDelay_lifecounter++ > 50) {
                        gameState = GAMING;
                        init();
                        stateDelay_lifecounter = 0;
                    }
                }
            }
            break;
            case GAMEOVER: {
                stateDelay_gameover++;

            }
            break;
            case FINISH: {
//                myMusic.play(ResourceTable.Media_finish, false);
                threadRunning = false;
                stateDelay_finish++;
            }
            break;
            case LOGO: {
                stateDelay_logo++;
                myMusic.stop();
            }
            break;
        }
    }

    /**
     * 超时处理
     */
    private void myTime() {
        if (time <= 0) {
            mario.setDead(true);
        }
    }

    /**
     * 更新分数
     *
     * @param scoreValue 分数值
     */
    private void updateScore(int scoreValue) {
        if (scoreValue > 0) {
            score += scoreValue;
            scoreString = "+" + scoreValue;
        }
        if (scoreValue < 0) {
            score -= scoreValue;
            scoreString = "-" + scoreValue;
        }

    }

    /**
     * 游戏进度控制
     */
    private void myStep() {
        if (tiledLayer_peng01.getX() == 0 && !isEnemyShown1) {
            for (int i = 0; i < 3; i++) {
                Enemy chestunt = new Chestunt(32, 32, chestuntBitmaps);
                chestunt.setVisiable(true);
                chestunt.setMirror(true);
                chestunt.setPosition(500 + 60 * i, 328);
                chestunts.add(chestunt);

            }
            for (int i = 0; i < 2; i++) {
                Turtle turtle = new Turtle(32, 48, turtleBitmaps);
                turtle.setVisiable(true);
                turtle.setMirror(false);
                turtle.setPosition(360 + 40 * i, 312);
                turtles.add(turtle);
            }
            isEnemyShown1 = true;

        }
        if (tiledLayer_peng01.getX() == -480 && !isEnemyShown2) {
            for (int i = 0; i < 3; i++) {
                Enemy enemy = new Chestunt(32, 32, chestuntBitmaps);
                enemy.setVisiable(true);
                enemy.setPosition(720 + 60 * i, 328);
                chestunts.add(enemy);
            }
            for (int i = 0; i < 2; i++) {
                Cannon cannon = new Cannon(40, 80, cannonBitmaps, mySoundPool);
                cannon.setVisiable(true);
                cannon.setPosition(800 + 80 * i, 160 - 40 * i);
                EnemyBullet enemyBullet = new EnemyBullet(enemyBulletBitmap);
                enemyBullet.setMirror(false);
                List<Sprite> bullets = new ArrayList<>();
                bullets.add(enemyBullet);
                cannon.setBullets(bullets);
                cannons.add(cannon);
            }

            isEnemyShown2 = true;
        }
        if (tiledLayer_peng01.getX() == -1000 && !isEnemyShown3) {
            for (int i = 0; i < 3; i++) {
                Enemy chestunt = new Chestunt(32, 32, chestuntBitmaps);
                chestunt.setVisiable(true);
                chestunt.setPosition(500 + 60 * i, 0);
                chestunts.add(chestunt);
            }
            isEnemyShown3 = true;
        }
        if (tiledLayer_peng01.getX() == -1400 && !isEnemyShown4) {
            for (int i = 0; i < 2; i++) {
                Turtle turtle = new Turtle(32, 48, turtleBitmaps);
                turtle.setVisiable(true);
                turtle.setMirror(false);
                turtle.setCanFly(true);
                turtle.setPosition(700 + 40 * i, 100);
                turtles.add(turtle);
            }
            Cannon cannon = new Cannon(40, 80, cannonBitmaps, mySoundPool);
            cannon.setVisiable(true);
            cannon.setPosition(640, 160);
            EnemyBullet enemyBullet = new EnemyBullet(enemyBulletBitmap);
            enemyBullet.setMirror(false);
            List<Sprite> bullets = new ArrayList<>();
            bullets.add(enemyBullet);
            cannon.setBullets(bullets);
            cannons.add(cannon);
            isEnemyShown4 = true;
        }
        if (tiledLayer_peng01.getX() == -2200 && !isEnemyShown5) {
            for (int i = 0; i < 7; i++) {
                Enemy chestunt = new Chestunt(32, 32, chestuntBitmaps);
                chestunt.setVisiable(true);
                chestunt.setPosition(360 + 40 * i, 240 - 40 * i);
                chestunts.add(chestunt);
            }
            isEnemyShown5 = true;
        }


        if (tiledLayer_peng01.getX() == -2800) {
            gameState = FINISH;
        }
    }

    /**
     * 处理移动与跳跃的逻辑
     */
    private void marioMove() {

        //1 死亡前先起跳
        if (mario.isDead()) {
            mario.move(0, mario.mSpeedY++);

        } else {
            if (mario.isRunning()) {
                //左移
                if (mario.isMirror()) {
                    mario.move(-4, 0);
                    //未到达屏幕中点
                } else if (mario.getX() < 400) {
                    mario.move(4, 0);
                    //越过屏幕中点 地图移动
                } else if (tiledLayer_peng01.getX() > 800 - tiledLayer_peng01.getCols()
                        * tiledLayer_peng01.getWidth()) {
                    tiledLayer_peng01.move(-4, 0);
                    tiledLayer_cover.move(-4, 0);
                    //到达地图边界 人物移动
                    spritesMove(chestunts, -4);
                    spritesMove(cannons, -4);
                    spritesMove(turtles, -4);
                    spritesMove(bricks, -4);
                    spritesMove(commonBricks, -4);
                    spritesMove(items, -4);
                    spritesMove(pipes, -4);


                } else {
                    mario.move(4, 0);
                }
            }
            if (mario.isJumping()) {
                mario.move(0, mario.mSpeedY++);
            }
        }
        if (mario.getY() > 400) {
            //2 再次落下才表示游戏结束
            if (!mario.isDead()) {
                mario.setDead(true);
                mario.setSpeedY(-16);
            } else {
                lifeNumber--;
                gameState = LIFTCOUNTER;
            }
        }


    }

    private void init() {
        isTransferReady = false;
        isTransferDone = true;
        isInited = false;
        threadRunning = false;
        isMaioDieSoundPlayed = false;
        time = 300;
        isEnemyShown1 = false;
        isEnemyShown2 = false;
        isEnemyShown3 = false;
        isEnemyShown4 = false;
        isEnemyShown5 = false;

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(60);
        mPaint.setStrokeCap(Paint.StrokeCap.ROUND_CAP);
        mPaint.setStyle(Paint.Style.STROKE_STYLE);
        mPaint.setTextSize(20);//设置字号
        /**
         * run方法重写
         */
        thread = new Thread() {
            @Override
            public void run() {
                System.out.println("this thread run()");
                //当显示过开场图片并且时间不为负数且线程停止标志位不为真时
                while (threadRunning && time > 0) {
//                    SystemClock.sleep(1000);
                    isTransferReady = false;
                    time--;
                    //刷新UI
//                    TaskDispatcher uiTaskDispatcher = context.getUITaskDispatcher();
//                    uiTaskDispatcher.asyncDispatch(() -> this.invalidate());
                    //刷新UI
                    InnerEvent event = InnerEvent.get(2, 1);
                    //注意：不要使用sendEvent(2),不记录event历史记录
                    handler.sendEvent(event);

//                    if (time == 100) {
//                        mySoundPool.play(mySoundPool.getHurryUpSound());
//
//                    }
                }

            }
        };
        //加载图片资源
//        aBitmap = getBitmap("button/a.png");
        aPixelMapHolder = getBitmap(ResourceTable.Media_button_a);//改成类型和名称的
//        aPixelMapHolder = getBitmap("button/a");
        bBitmap = getBitmap(ResourceTable.Media_button_b);
        leftBitmap = getBitmap(ResourceTable.Media_button_left);
        rightBitmap = getBitmap(ResourceTable.Media_button_right);
        downBitmap = getBitmap(ResourceTable.Media_button_down);
        gameoverbitmap = getBitmap(ResourceTable.Media_logo_gameover);
//        marioBitmap = getBitmap("base/mario/small/mario_01.png");
        marioBitmap = getBitmap(ResourceTable.Media_small_mario_01);
        logoBitmap = getBitmap(ResourceTable.Media_logo_logo);
        finishBitmap = getBitmap(ResourceTable.Media_logo_finish);
//        PixelMapHolder mushroomBitmap = getBitmap("base/item/mushroom.png");
        PixelMapHolder mushroomBitmap = getBitmap(ResourceTable.Media_mushroom);
//        coinBitmap = getBitmap("base/item/coin/coin_00.png");
        coinBitmap = getBitmap(ResourceTable.Media_item_coin_00);
        PixelMapHolder cannonBitmap = getBitmap(ResourceTable.Media_enemy_cannon);
//        enemyBulletBitmap = getBitmap("base/enemy/enemy_bullet.png");
        enemyBulletBitmap = getBitmap(ResourceTable.Media_enemy_bullet);
//        PixelMapHolder pipeBitmap = getBitmap("base/brick/pipe.png");
        PixelMapHolder pipeBitmap = getBitmap(ResourceTable.Media_brick_pipe);

        cannons = new ArrayList<>();
        turtles = new ArrayList<>();
        List<PixelMapHolder> marioSmallBitmaps = new ArrayList<>();
        ArrayList<PixelMapHolder> marioFireInvBitmaps = new ArrayList<>();
        ArrayList<PixelMapHolder> marioSmallInvBitmaps = new ArrayList<>();
        ArrayList<PixelMapHolder> marioBigInvincibleBitmaps = new ArrayList<>();
        commonBricks = new ArrayList<>();
        List<PixelMapHolder> commonbrickbitmaps = new ArrayList<>();
        bricks = new ArrayList<>();
        items = new ArrayList<>();
        List<PixelMapHolder> marioBigBitmaps = new ArrayList<>();
        List<PixelMapHolder> marioFireBitmaps = new ArrayList<>();
        List<PixelMapHolder> flowerBitmaps = new ArrayList<>();
        List<PixelMapHolder> brokenBitmaps = new ArrayList<>();
        List<PixelMapHolder> pipeBitmaps = new ArrayList<>();
        fireBitmaps = new ArrayList<>();
        cannonBitmaps = new ArrayList<>();
        pipes = new ArrayList<>();

        //设置数组型图片资源
//      marioBigBitmaps.add(getBitmap("base/mario/big/mario_0" + i + ".png"));
        marioBigBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_big_mario_00)));
        marioBigBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_big_mario_01)));
        marioBigBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_big_mario_02)));
        marioBigBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_big_mario_03)));
        marioBigBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_big_mario_04)));
        marioBigBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_big_mario_05)));
        marioBigBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_big_mario_06)));
        HiLog.info(LABEL_LOG,"marioBigBitmaps" + marioBigBitmaps.size());


//      marioSmallBitmaps.add(getBitmap("base/mario/small/mario_0" + i + ".png"));
        marioSmallBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_small_mario_00)));
        marioSmallBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_small_mario_01)));
        marioSmallBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_small_mario_02)));
        marioSmallBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_small_mario_03)));
        marioSmallBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_small_mario_04)));
        marioSmallBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_small_mario_05)));
        marioSmallBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_small_mario_06)));
        HiLog.info(LABEL_LOG,"marioSmallBitmaps" + marioSmallBitmaps.size());

//      marioFireBitmaps.add(getBitmap("base/mario/fire/mario_0" + i + ".png"));
        marioFireBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_fire_mario_00)));
        marioFireBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_fire_mario_01)));
        marioFireBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_fire_mario_02)));
        marioFireBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_fire_mario_03)));
        marioFireBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_fire_mario_04)));
        marioFireBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_fire_mario_05)));
        marioFireBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_fire_mario_06)));
        HiLog.info(LABEL_LOG,"marioFireBitmaps" + marioFireBitmaps.size());


//      marioFireInvBitmaps.add(getBitmap("base/mario/fire/mario_inv_0" + i + ".png"));
        marioFireInvBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_fire_mario_inv_00)));
        marioFireInvBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_fire_mario_inv_01)));
        marioFireInvBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_fire_mario_inv_02)));
        marioFireInvBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_fire_mario_inv_03)));
        marioFireInvBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_fire_mario_inv_04)));
        marioFireInvBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_fire_mario_inv_05)));
        marioFireInvBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_fire_mario_inv_06)));
        HiLog.info(LABEL_LOG,"marioFireInvBitmaps" + marioFireInvBitmaps.size());

//      marioSmallInvBitmaps.add(getBitmap("base/mario/small/mario_inv_0" + i + ".png"));
        marioSmallInvBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_small_mario_inv_00)));
        marioSmallInvBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_small_mario_inv_01)));
        marioSmallInvBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_small_mario_inv_02)));
        marioSmallInvBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_small_mario_inv_03)));
        marioSmallInvBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_small_mario_inv_04)));
        marioSmallInvBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_small_mario_inv_05)));
        marioSmallInvBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_small_mario_inv_06)));
        HiLog.info(LABEL_LOG,"marioSmallInvBitmaps" + marioSmallInvBitmaps.size());

//      marioBigInvincibleBitmaps.add(getBitmap("base/mario/big/mario_inv_0" + i + ".png"));
        marioBigInvincibleBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_big_mario_inv_00)));
        marioBigInvincibleBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_big_mario_inv_01)));
        marioBigInvincibleBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_big_mario_inv_02)));
        marioBigInvincibleBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_big_mario_inv_03)));
        marioBigInvincibleBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_big_mario_inv_04)));
        marioBigInvincibleBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_big_mario_inv_05)));
        marioBigInvincibleBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_big_mario_inv_06)));
        HiLog.info(LABEL_LOG,"marioBigInvincibleBitmaps" + marioBigInvincibleBitmaps.size());

        pipeBitmaps.add(pipeBitmap);
        HiLog.info(LABEL_LOG,"pipeBitmaps" + pipeBitmaps.size());

//      brokenBitmaps.add(getBitmap(String.format(Locale.CHINA,
//              "base/brick/broken/broken_%02d.png", i)));
        brokenBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_broken_00)));
        brokenBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_broken_01)));
        brokenBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_broken_02)));
        brokenBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_broken_03)));
        brokenBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_broken_04)));
        brokenBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_broken_05)));
        brokenBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_broken_06)));
        brokenBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_broken_07)));
        brokenBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_broken_08)));
        brokenBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_broken_09)));
        HiLog.info(LABEL_LOG,"brokenBitmaps" + brokenBitmaps.size());


        chestuntBitmaps = new ArrayList<>();
//      chestuntBitmaps.add(getBitmap("base/enemy/chestunt/chestunt_0" + i + ".png"));
        chestuntBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_enemy_chestunt_00)));
        chestuntBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_enemy_chestunt_01)));
        chestuntBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_enemy_chestunt_02)));
        HiLog.info(LABEL_LOG,"chestuntBitmaps" + chestuntBitmaps.size());

        chestunts = new ArrayList<>();
        List<PixelMapHolder> coinBitmaps = new ArrayList<>();

        brokenBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_item_coin_00)));
        brokenBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_item_coin_01)));
        brokenBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_item_coin_02)));
        brokenBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_item_coin_03)));
        brokenBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_item_coin_04)));
        HiLog.info(LABEL_LOG,"brokenBitmaps" + brokenBitmaps.size());

        flowerBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_item_flower_00)));
        flowerBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_item_flower_01)));
        flowerBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_item_flower_02)));
        flowerBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_item_flower_03)));
        HiLog.info(LABEL_LOG,"flowerBitmaps" + flowerBitmaps.size());

        fireBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_item_fire_00)));
        fireBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_item_fire_01)));
        fireBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_item_fire_02)));
        fireBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_item_fire_03)));
        HiLog.info(LABEL_LOG,"fireBitmaps" + fireBitmaps.size());

        cannonBitmaps.add(cannonBitmap);
        HiLog.info(LABEL_LOG,"cannonBitmaps" + cannonBitmaps.size());

        List<PixelMapHolder> brickBitmaps = new ArrayList<>();
        brickBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_brick_00)));
        brickBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_brick_01)));
        brickBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_brick_02)));
        brickBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_brick_03)));
        brickBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_brick_04)));
        HiLog.info(LABEL_LOG,"brickBitmaps" + brickBitmaps.size());

        turtleBitmaps = new ArrayList<>();
        turtleBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_enemy_turtle_00)));
        turtleBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_enemy_turtle_01)));
        turtleBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_enemy_turtle_02)));
        turtleBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_enemy_turtle_03)));
        turtleBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_enemy_turtle_04)));
        HiLog.info(LABEL_LOG,"turtleBitmaps" + turtleBitmaps.size());

        List<PixelMapHolder> starBitmaps = new ArrayList<>();
        starBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_item_star_00)));
        starBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_item_star_01)));
        starBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_item_star_02)));
        starBitmaps.add(new PixelMapHolder(getPixelMapFromResource(ResourceTable.Media_item_star_03)));
        HiLog.info(LABEL_LOG,"starBitmaps" + starBitmaps.size());

        int[][] pipePositions = {
                {37, 7}, {62, 7}
        };

        for (int i = 0; i < 2; i++) {
            Pipe pipe = new Pipe(80, 80, pipeBitmaps);
            pipe.setVisiable(true);
            if (i == 0) {
                pipe.setTransfer(true);
            }
            pipe.setPosition(pipePositions[i][0] * 40, pipePositions[i][1] * 40);
            pipes.add(pipe);
        }

        //存放问号砖块行列坐标
        int[][] itemBrickPositions = {
                {3, 3}, {3, 6}, {56, 6}, {12, 5}
        };
        //存放金币坐标
        int[][] coinsPostions = {
                {10, 0}, {10, 1}, {10, 2}, {10, 3}, {10, 4}, {11, 2}, {12, 0}, {12, 1}, {12, 2}, {12, 3}, {12, 4},
                {14, 0}, {14, 1}, {14, 2}, {14, 3}, {14, 4}, {15, 0}, {16, 0}, {15, 2}, {16, 2}, {15, 4}, {16, 4},
                {18, 0}, {18, 1}, {18, 2}, {18, 3}, {18, 4}, {19, 4}, {20, 4},
                {22, 0}, {22, 1}, {22, 2}, {22, 3}, {22, 4}, {23, 4}, {24, 4},
                {26, 1}, {26, 2}, {26, 3}, {28, 1}, {28, 2}, {28, 3}, {27, 0}, {27, 4},
                {36, 5}, {37, 5}, {38, 5}, {39, 5}, {40, 5}, {41, 5}, {42, 5}, {43, 5}

        };

        //创建金币
        for (int[] coinposition : coinsPostions) {
            Coin coin = new Coin(40, 40, coinBitmaps);
            coin.setVisiable(true);
            coin.setPosition(coinposition[0] * 40, coinposition[1] * 40);
            items.add(coin);
        }

        //存放普通砖块坐标
        int[][] commonBrickPostions = {
                {2, 6}, {4, 6}, {29, 7}, {30, 7}, {32, 6}, {34, 5}, {36, 3}, {43, 3}, {36, 4},
                {37, 4}, {38, 4}, {39, 4}, {40, 4}, {41, 4}, {42, 4}, {43, 4}, {11, 5}, {13, 5},
                {14, 5}, {21, 6}, {22, 6}, {50, 6}, {51, 6}, {52, 6}, {53, 6}, {54, 6}

        };

        //创建砖块
        Brick brick = new Brick(40, 40, brickBitmaps);
        brick.setPosition(40 * itemBrickPositions[0][0], 40 * itemBrickPositions[0][1]);
//        brick.createItem(true, flowerBitmaps, ItemType.Flower);
        items.add(brick.getItemSprite());
        brick.setVisiable(true);
        bricks.add(brick);
        Brick brick2 = new Brick(40, 40, brickBitmaps);
        brick2.setPosition(40 * itemBrickPositions[1][0], 40 * itemBrickPositions[1][1]);

//        brick2.createItem(true, mushroomBitmap, ItemType.Mushroom);
        items.add(brick2.getItemSprite());
        brick2.setVisiable(true);
        bricks.add(brick2);

        Brick brick3 = new Brick(40, 40, brickBitmaps);
        brick3.setPosition(40 * itemBrickPositions[2][0], 40 * itemBrickPositions[2][1]);
//        brick3.createItem(true, starBitmaps, ItemType.Star);
        items.add(brick3.getItemSprite());
        brick3.setVisiable(true);
        bricks.add(brick3);

        Brick brick4 = new Brick(40, 40, brickBitmaps);
        brick4.setPosition(40 * itemBrickPositions[3][0], 40 * itemBrickPositions[3][1]);
//        brick4.createItem(true, flowerBitmaps, ItemType.Flower);
        items.add(brick4.getItemSprite());
        brick4.setVisiable(true);
        bricks.add(brick4);

        commonbrickbitmaps.add(getBitmap(ResourceTable.Media_brick_commonBrick));

        for (int[] position : commonBrickPostions) {
            CommonBrick commonBrick = new CommonBrick(40, 40, commonbrickbitmaps);
            commonBrick.setVisiable(true);
            Broken broken = new Broken(187, 187, brokenBitmaps);
            commonBrick.setBroken(broken);
            commonBrick.setCanBroken(false);
            commonBrick.setPosition(40 * position[0], 40 * position[1]);
            commonBricks.add(commonBrick);
        }

        rectF = new RectFloat();
        //初始化玛丽
        this.mario = new Mario(32, 32, marioSmallBitmaps);
        this.mario.setVisiable(true);
        this.mario.setPosition(0, 330);
        List<List<PixelMapHolder>> bitmapsList = new ArrayList<>();
        bitmapsList.add(marioSmallBitmaps);
        bitmapsList.add(marioBigBitmaps);
        bitmapsList.add(marioFireBitmaps);
        bitmapsList.add(marioSmallInvBitmaps);
        bitmapsList.add(marioBigInvincibleBitmaps);
        bitmapsList.add(marioFireInvBitmaps);
        this.mario.setBitmapsList(bitmapsList);

        //初始化地图
        int map_peng01[][] = getMapArray();
        int map_cover[][] = getMapCoverArray();
        PixelMapHolder mapBitmap = getBitmap(ResourceTable.Media_map1);
        tiledLayer_peng01 = new TiledLayer(mapBitmap, 100, 12, 40, 40);
        tiledLayer_cover = new TiledLayer(mapBitmap, 100, 12, 40, 40);
        tiledLayer_peng01.setTiledCell(map_peng01);
        tiledLayer_cover.setTiledCell(map_cover);
        if (isFirstRun) {
            gameState = LOGO;
            isFirstRun = false;
        } else {
            gameState = GAMING;
        }
        isInited = true;
    }

    //画出游戏界面
    private void myDraw() {
        try {
            System.out.println("================================gameState==================================" + gameState);
            switch (gameState) {
                case LOGO: {
                    lockCanvas.drawPixelMapHolder(logoBitmap, 0, 0, mPaint);
                }
                break;
                case GAMEOVER: {

                    lockCanvas.drawColor(Color.getIntColor("#000000"),BlendMode.COLOR);
                    lockCanvas.drawPixelMapHolder(gameoverbitmap,
                            400 - gameoverbitmap.getPixelMap().getImageInfo().size.width / 2,
                            250 - gameoverbitmap.getPixelMap().getImageInfo().size.height / 2, mPaint);

                }
                break;
                case FINISH: {
                    lockCanvas.drawPixelMapHolder(finishBitmap, 0, 0, mPaint);
                }
                break;
                case LIFTCOUNTER: {
                    lockCanvas.drawColor(Color.getIntColor("#000000"),BlendMode.COLOR);
                    lockCanvas.drawPixelMapHolder(marioBitmap, 400 - marioBitmap.getPixelMap().getImageInfo().size.width,
                            250 - marioBitmap.getPixelMap().getImageInfo().size.height, mPaint);
                    lockCanvas.drawText(mPaint,
                            String.format(Locale.CHINA, "X %02d", lifeNumber),
                            440 - marioBitmap.getPixelMap().getImageInfo().size.width, 285 - marioBitmap.getPixelMap().getImageInfo().size.height);
                }
                break;
                case TRANSFER:
                case GAMING: {
                    System.out.println("================================GAMING()==================================");
                    tiledLayer_cover.draw(lockCanvas);
                    tiledLayer_peng01.draw(lockCanvas);
                    System.out.println("bricks====================" + bricks.size());
                    spritesDraw(bricks, lockCanvas);
                    spritesDraw(commonBricks, lockCanvas);
                    spritesDraw(chestunts, lockCanvas);
                    spritesDraw(cannons, lockCanvas);
                    spritesDraw(turtles, lockCanvas);
//
//                    spritesDraw(items, lockCanvas);

                    mario.draw(lockCanvas);
                    spritesDraw(pipes, lockCanvas);
//                    lockCanvas.drawBitmap(aBitmap, 680, 420, null);
                    lockCanvas.drawPixelMapHolder(aPixelMapHolder,680,420,mPaint);
                    lockCanvas.drawPixelMapHolder(bBitmap, 740, 360, mPaint);
                    lockCanvas.drawPixelMapHolder(leftBitmap, 0, 360, mPaint);
                    lockCanvas.drawPixelMapHolder(rightBitmap, 120, 360, mPaint);
                    lockCanvas.drawPixelMapHolder(downBitmap, 60, 420, mPaint);
                    //绘制单次得分
                    if (scoreString != null) {
                        lockCanvas.drawText(mPaint,scoreString, mario.getX() + 25, mario.getY() - 20 - 3 * delay);
                        if (delay++ == 6) {
                            scoreString = null;
                            delay = 0;
                        }
                    }
                    if (mario.getBullets() != null) {
                        int useableBulletCount = mario.getUseableBulletCount();
                        lockCanvas.drawText(mPaint,"Bullets Ready:" + useableBulletCount, 500, 30);
                        if (mario.isInvincible()) {
//                            canvas.drawText(paint,"Invincible Time:" + mario.getInvincibleTime(), 500,
//                                    60);
                            if (isTransferReady) {
                                lockCanvas.drawText(mPaint,"Transfer Ready", 500, 90);
                            }
                        } else {
                            if (mario.isZeroDamage()) {
                                lockCanvas.drawText(mPaint,"ZeroDamage Time:" + mario.getZeroDamageTime(), 500,
                                        60);
                                if (isTransferReady) {
                                    lockCanvas.drawText(mPaint,"Transfer Ready", 500, 90);
                                }
                            } else {
                                if (isTransferReady) {
                                    lockCanvas.drawText(mPaint,"Transfer Ready", 500, 60);
                                }
                            }
                        }
                    } else {
                        if (mario.isInvincible()) {
                            lockCanvas.drawText(mPaint,"Invincible Time:" + mario.getInvincibleTime(), 500,
                                    30);
                            if (isTransferReady) {
                                lockCanvas.drawText(mPaint,"Transfer Ready", 500, 60);
                            }
                        } else {
                            if (mario.isZeroDamage()) {
                                lockCanvas.drawText(mPaint,"ZeroDamage Time:" + mario.getZeroDamageTime(), 500,
                                        30);
                                if (isTransferReady) {
                                    lockCanvas.drawText(mPaint,"Transfer Ready", 500, 60);
                                }
                            } else {
                                if (isTransferReady) {
                                    lockCanvas.drawText(mPaint,"Transfer Ready", 500, 30);
                                }
                            }
                        }
                    }
                    //绘制总分数
                    lockCanvas.drawText(mPaint,String.format(Locale.CHINA, "SCORE:%06d", score), 30, 30);
                    if (mario.getBullets() != null) {
                        for (int i = 0; i < mario.getBullets().size(); i++) {
                            mario.getBullets().get(i).draw(lockCanvas);
                        }
                    }
                    //绘制金币数
                    lockCanvas.drawPixelMapHolder(coinBitmap, 200, 0, mPaint);
                    lockCanvas.drawText(mPaint,String.format(Locale.CHINA, "x %02d", coinNumber), 240,
                            30);
                    //绘制剩余时间
                    lockCanvas.drawText(mPaint,String.format(Locale.CHINA, "TIME:%03d", time), 700, 30);
                }
                break;
            }
            lockCanvas.restore();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (lockCanvas != null){
//                holder.unlockCanvasAndPost(lockCanvas);
            }

        }
    }

    /**
     * 玛丽奥与砖块碰撞
     */
    private void myCollision() {
        //玛丽与普通砖块
        sptiteCollisionSprite(mario, commonBricks);
        //玛丽与问号砖块
        sptiteCollisionSprite(mario, bricks);

        sptiteCollisionSprite(mario, pipes);

        //玛丽与大炮
        sptiteCollisionSprite(mario, cannons);
        //道具与砖块碰撞
        sptitesCollisionSprites(items, bricks);
        //道具与普通砖块碰撞
        sptitesCollisionSprites(items, commonBricks);
        sptitesCollisionSprites(items, pipes);

        //大炮与砖块碰撞
        sptitesCollisionSprites(cannons, bricks);
        sptitesCollisionSprites(cannons, commonBricks);
        sptitesCollisionSprites(cannons, pipes);
        //乌龟与砖块碰撞
        sptitesCollisionSprites(turtles, bricks);
        //乌龟与问号砖块
        sptitesCollisionSprites(turtles, commonBricks);
        sptitesCollisionSprites(turtles, pipes);
        //敌人与砖块
        sptitesCollisionSprites(chestunts, bricks);
        sptitesCollisionSprites(chestunts, commonBricks);
        sptitesCollisionSprites(chestunts, pipes);
        //玛丽子弹与砖块
        sptitesCollisionSprites(mario.getBullets(), bricks);
        //龟壳与敌人碰撞
        sptitesCollisionSprites(mario.getBullets(), commonBricks);
        sptitesCollisionSprites(mario.getBullets(), pipes);
        turtleShellCollisionWith(turtles);
        turtleShellCollisionWith(chestunts);

        if (!mario.isDead()) {
            //玛丽与板栗碰撞
            marioCollisionWith(chestunts);
            marioCollisionWith(turtles);
            marioCollisionWith(items);
            marioCollisionWith(pipes);
            //玛丽与大炮子弹碰撞
            if (cannons != null) {
                for (int w = 0; w < cannons.size(); w++) {
                    List<Sprite> bullets = ((Cannon) cannons.get(w)).getBullets();
                    marioCollisionWith(bullets);
                }
            }
        }
    }

    /**
     * 精灵与地图碰撞
     * 玛丽
     * 敌人
     * 砖块道具
     */
    private void collisionWithMap() {
        //玛丽碰撞
        if (!mario.isDead()) {
            spriteCollisionMap(mario);
            spritesCollisionMap(mario.getBullets());
        }
        //敌人碰撞
        spritesCollisionMap(chestunts);
        spritesCollisionMap(turtles);
        spritesCollisionMap(cannons);
        //道具与地图碰撞
        spritesCollisionMap(items);
    }

    //碰撞
    //精灵与精灵
    //玛丽与敌人
    //精灵与地图
    private void turtleShellCollisionWith(List<Sprite> sprites) {
        if (sprites != null && turtles != null) {
            for (int i = 0; i < turtles.size(); i++) {
                Sprite shell = turtles.get(i);
                if (shell.isVisiable() && shell.isDead()) {
                    turtleShellCollisionWith(shell, sprites);
                }
            }
        }
    }

    private void turtleShellCollisionWith(Sprite shell, List<Sprite> sprites) {
        if (sprites != null) {
            for (int i = 0; i < sprites.size(); i++) {
                turtleShellCollisionWith(shell, sprites.get(i));
            }

        }
    }

    /**
     * 龟壳碰撞
     * @param shell  龟壳
     * @param sprite 目标精灵
     */
    private void turtleShellCollisionWith(Sprite shell, Sprite sprite) {
        if (shell.isVisiable() && shell.isRunning() && !sprite.isDead()) {
            if (shell.collisionWith(sprite)) {
                if (sprite instanceof Turtle) {
                    if (!((Turtle) sprite).isZeroDamage()) {
                        if (((Turtle) sprite).isCanFly()) {
                            ((Turtle) sprite).setCanFly(false);
                            ((Turtle) sprite).setZeroDamage(true);
                            updateScore(100);
                        } else {
//                            mySoundPool.play(mySoundPool.getHitEnemySound());
                            ((Turtle) sprite).setOverturn(true);
                            if (shell.getX() > sprite.getX()) {
                                sprite.setMirror(false);
                            } else {
                                sprite.setMirror(true);
                            }
                            sprite.setSpeedY(-10);
                            sprite.setDead(true);
                            updateScore(100);
                        }
                    }
                } else {
//                    mySoundPool.play(mySoundPool.getHitEnemySound());
                    ((Enemy) sprite).setOverturn(true);
                    if (shell.getX() > sprite.getX()) {
                        sprite.setMirror(false);
                    } else {
                        sprite.setMirror(true);
                    }
                    sprite.setSpeedY(-10);
                    sprite.setDead(true);
                    updateScore(100);
                }


            }
        }
    }

    /**
     * 玛丽与一组精灵
     *
     * @param sprites 精灵数组
     */
    private void marioCollisionWith(List<Sprite> sprites) {
        if (sprites != null) {
            for (int i = 0; i < sprites.size(); i++) {
                Sprite sprite = sprites.get(i);
                marioCollisionWith(sprite);
                if (!(sprite instanceof ItemSprite)) {
                    marioBulletCollisionWithEnemy(sprite);
                }
            }
        }
    }


    /**
     * 玛丽与精灵碰撞
     *
     * @param sprite 单个精灵
     */
    private void marioCollisionWith(Sprite sprite) {
        if (mario.collisionWith(sprite)) {
            if (sprite instanceof Pipe) {
                if (((Pipe) sprite).isTransfer()) {
                    isTransferReady = true;
                }
            } else
                //与道具碰撞
                if (sprite instanceof ItemSprite) {
                    //与大炮子弹
                    if (sprite instanceof EnemyBullet) {
                        if (mario.isInvincible()) {
                            updateScore(100);
                            sprite.setVisiable(false);
                        } else if (mario.isJumping() && mario.siteCollisionWith(sprite, 下) &&
                                !sprite.isDead()) {
//                            mySoundPool.play(mySoundPool.getHitEnemySound());
                            //杀死敌人时获得100积分
                            updateScore(100);
                            mario.setSpeedY(-10);
                            sprite.setVisiable(false);
                        } else {
                            if (!mario.isZeroDamage() && sprite.isVisiable()) {
                                int status = mario.getStatus();
                                if (status != 0) {
                                    mario.shapeShift(status - 1);
//                                    mySoundPool.play(mySoundPool.getHurtSound());
                                    mario.setZeroDamage(true);
                                } else {
                                    mario.setDead(true);
                                    mario.setSpeedY(-16);
                                }
                            }
                        }
                    } else {
                        int sound = mySoundPool.getItemSound();
                        int status = mario.getStatus();
                        if (sprite instanceof Mushroom) {
//                            mySoundPool.play(mySoundPool.getItemSound());
                            if (status == 0) {
                                mario.shapeShift(1);
                            }
                        }
                        if (sprite instanceof Flower) {
                            //花
                            if (status != 2) {
                                //非第三状态都执行
                                mario.shapeShift(2);
                                //设置弹夹
                                List<Sprite> bullets = new ArrayList<>();
                                for (int j = 0; j < 5; j++) {
                                    Bullet bullet = new Bullet(16, 16, fireBitmaps);
                                    if (mario.isMirror()) {
                                        bullet.setMirror(false);
                                    } else {
                                        bullet.setMirror(true);
                                    }
                                    bullets.add(bullet);
                                }
                                mario.setBullets(bullets);
                            }
                        }
                        if (sprite instanceof Star) {
                            //玛丽吃到无敌星后变成无敌状态
                            mario.setInvincible(true);
                        }
                        if (sprite instanceof Coin) {
                            coinNumber++;
                            sound = mySoundPool.getCoinSound();
                        }
//                        mySoundPool.play(sound);
                        sprite.setVisiable(false);
                        updateScore(100);
                    }
                } else {
                    //与敌人碰撞
                    if (!sprite.isDead()) {
                        if (mario.isInvincible()) {
                            //如果乌龟带翅膀则切换状态
                            if (sprite instanceof Turtle) {
                                //乌龟在带翅膀状态被击中有免伤时间
                                if (!((Turtle) sprite).isZeroDamage()) {
                                    if (((Turtle) sprite).isCanFly()) {
                                        ((Turtle) sprite).setCanFly(false);
                                        sprite.setSpeedY(0);
                                        ((Turtle) sprite).setZeroDamage(true);

                                        updateScore(100);
                                        mario.setSpeedY(-10);
                                        mario.setJumping(true);
                                    } else {
                                        ((Enemy) sprite).setOverturn(true);
                                        sprite.setDead(true);
//                                        mySoundPool.play(mySoundPool.getHitEnemySound());
                                        updateScore(100);
                                        mario.setSpeedY(-10);
                                        mario.setJumping(true);
                                    }
                                }
                            } else {
                                ((Enemy) sprite).setOverturn(true);
                                sprite.setSpeedY(-10);
                                sprite.setDead(true);
//                                mySoundPool.play(mySoundPool.getHitEnemySound());
                                //杀死敌人时获得100积分
                                updateScore(100);
                            }
                        } else if (mario.isJumping()) {
                            if (sprite instanceof Turtle) {
                                //乌龟在带翅膀状态被击中有免伤时间
                                if (!((Turtle) sprite).isZeroDamage()) {
                                    if (((Turtle) sprite).isCanFly()) {
                                        ((Turtle) sprite).setCanFly(false);
                                        sprite.setSpeedY(-3);
                                        ((Turtle) sprite).setZeroDamage(true);
                                        updateScore(100);
                                        mario.setSpeedY(-10);
                                    } else {
                                        sprite.setDead(true);
//                                        mySoundPool.play(mySoundPool.getHitEnemySound());
                                        updateScore(100);
                                    }
                                }
                            } else {
                                sprite.setDead(true);
//                                mySoundPool.play(mySoundPool.getHitEnemySound());
                                //杀死敌人时获得100积分
                                updateScore(100);
                                mario.setSpeedY(-10);
                            }
                        } else {
                            if (!mario.isZeroDamage()) {
                                int status = mario.getStatus();
                                if (status != 0) {
                                    mario.shapeShift(status - 1);
//                                    mySoundPool.play(mySoundPool.getHurtSound());
                                    mario.setZeroDamage(true);
                                } else {
                                    mario.setDead(true);
                                    mario.setSpeedY(-16);
                                }
                            }
                        }
                        //乌龟壳
                    } else if (sprite instanceof Turtle) {
                        if (!sprite.isRunning()) {
                            if (mario.getX() < sprite.getX()) {
                                sprite.setMirror(true);
                                sprite.setRunning(true);
                            } else {
                                sprite.setMirror(false);
                                sprite.setRunning(true);
                            }
                            mario.setJumping(true);
                            mario.setSpeedY(-16);
                        } else {
                            if (!mario.isZeroDamage() && !mario.isJumping()) {
                                sprite.setVisiable(false);
                                int status = mario.getStatus();
                                if (status != 0) {
                                    mario.shapeShift(status - 1);
//                                    mySoundPool.play(mySoundPool.getHurtSound());
                                    mario.setZeroDamage(true);
                                } else {
                                    mario.setDead(true);
                                    mario.setSpeedY(-16);
                                }
                            }
                        }
                    }
                }
        }
    }

    private void marioBulletCollisionWithEnemy(Sprite sprite) {
        List<Sprite> bullets = mario.getBullets();
        if (bullets != null) {
            for (int i = 0; i < bullets.size(); i++) {
                Sprite sprite1 = bullets.get(i);
                //玛丽子弹不能消灭敌人子弹
                if (sprite1.isVisiable() && sprite1.collisionWith(sprite) && !(sprite instanceof
                        EnemyBullet) && !sprite.isDead()) {
                    //乌龟在带翅膀状态被击中有免伤时间
                    if (sprite instanceof Turtle) {
                        if (!((Turtle) sprite).isZeroDamage()) {
                            if (((Turtle) sprite).isCanFly()) {
                                ((Turtle) sprite).setCanFly(false);
                                ((Turtle) sprite).setZeroDamage(true);
                                updateScore(100);
                            } else {
                                sprite.setDead(true);
                                ((Turtle) sprite).setOverturn(true);
                                if (sprite1.getX() > sprite.getX()) {
                                    sprite.setMirror(false);
                                } else {
                                    sprite.setMirror(true);
                                }
                                sprite.setSpeedY(-10);
//                                mySoundPool.play(mySoundPool.getHitEnemySound());
                                updateScore(100);
                            }
                        }
                        sprite1.setVisiable(false);
                    } else {
//                        mySoundPool.play(mySoundPool.getHitEnemySound());
                        sprite1.setVisiable(false);
//                        sprite.setVisiable(false);
//                        sprite.setDead(true);
                        ((Enemy) sprite).setOverturn(true);
                        if (sprite1.getX() > sprite.getX()) {
                            sprite.setMirror(false);
                        } else {
                            sprite.setMirror(true);
                        }
                        sprite.setSpeedY(-10);
                        sprite.setDead(true);
                        updateScore(100);
                    }
                }
            }
        }
    }

    /**
     * 精灵与地图（碰撞层）碰撞
     *
     * @param sprites 精灵数组
     */

    private void spritesCollisionMap(List<Sprite> sprites) {
        if (sprites != null) {
            for (int i = 0; i < sprites.size(); i++) {
                spriteCollisionMap(sprites.get(i));
            }
        }
    }

    /**
     * 精灵与地图（碰撞层）碰撞
     *
     * @param sprite 单个精灵
     */
    private void spriteCollisionMap(Sprite sprite) {
        if (sprite.isVisiable() && !sprite.isDead()) {
            //头顶碰撞
            if (sprite.siteCollisionWith(tiledLayer_peng01, 上左) ||
                    sprite.siteCollisionWith(tiledLayer_peng01, 上中) ||
                    sprite.siteCollisionWith(tiledLayer_peng01, 上右)) {
                if (sprite instanceof Bullet) {
                    sprite.setVisiable(false);
                } else {
                    sprite.setSpeedY(Math.abs(sprite.getSpeedY()));
                }
            }
            //脚部碰撞
            if (sprite.siteCollisionWith(tiledLayer_peng01, 下左) ||
                    sprite.siteCollisionWith(tiledLayer_peng01, 下中) ||
                    sprite.siteCollisionWith(tiledLayer_peng01, 下右)) {
                //乌龟特殊处理
                if (sprite instanceof Turtle) {
                    if (((Turtle) sprite).isCanFly()) {
                        sprite.setJumping(true);
                        sprite.setSpeedY(-10);
                    } else {
                        sprite.setJumping(false);
                        int footY = sprite.getY() + sprite.getHeight();
                        int newY = footY / tiledLayer_peng01.getHeight()
                                * tiledLayer_peng01.getHeight()
                                - sprite.getHeight();
                        sprite.setPosition(sprite.getX(), newY);
                    }
                    //子弹特殊处理
                } else if (sprite instanceof Bullet || sprite instanceof Star) {
                    sprite.setSpeedY(-10);
                    sprite.setJumping(true);
                } else {
                    sprite.setJumping(false);
                    //坐标修正
                    //取得脚部坐标
                    int footY = sprite.getY() + sprite.getHeight();
                    //取整
                    int newY = footY / tiledLayer_peng01.getHeight()
                            * tiledLayer_peng01.getHeight()
                            - sprite.getHeight();
                    sprite.setPosition(sprite.getX(), newY);
                }
            }
            //落地判断
            else if (!sprite.isJumping()) {
                sprite.setJumping(true);
                sprite.setSpeedY(0);
            }
            //左边碰撞
            if (sprite.siteCollisionWith(tiledLayer_peng01, 左上) ||
                    sprite.siteCollisionWith(tiledLayer_peng01, 左中) ||
                    sprite.siteCollisionWith(tiledLayer_peng01, 左下)) {
                if (sprite instanceof Bullet) {
                    sprite.setVisiable(false);
                } else {
                    sprite.setMirror(true);
                    sprite.move(4, 0);
                }
            }
            //右边碰撞
            if (sprite.siteCollisionWith(tiledLayer_peng01, 右上) ||
                    sprite.siteCollisionWith(tiledLayer_peng01, 右中) ||
                    sprite.siteCollisionWith(tiledLayer_peng01, 右下)) {
                if (sprite instanceof Bullet) {
                    sprite.setVisiable(false);
                } else {
                    sprite.setMirror(false);
                    sprite.move(-4, 0);
                }
            }
            //乌龟壳
        } else if (sprite.isVisiable() && sprite.isDead() && sprite instanceof Turtle) {
            if (!((Turtle) sprite).isOverturn()) {
                //脚部碰撞
                if (sprite.siteCollisionWith(tiledLayer_peng01, 下左) ||
                        sprite.siteCollisionWith(tiledLayer_peng01, 下中) ||
                        sprite.siteCollisionWith(tiledLayer_peng01, 下右)) {
                    //乌龟特殊处理
                    sprite.setJumping(false);
                    //坐标修正
                    //取得脚部坐标
                    int footY = sprite.getY() + sprite.getHeight();
                    //取整
                    int newY = footY / tiledLayer_peng01.getHeight()
                            * tiledLayer_peng01.getHeight()
                            - sprite.getHeight();
                    sprite.setPosition(sprite.getX(), newY);
                }
            }
            //落地判断
            else if (!sprite.isJumping()) {
                sprite.setJumping(true);
                sprite.setSpeedY(0);
            }
            //左边碰撞
            if (sprite.siteCollisionWith(tiledLayer_peng01, 左上) ||
                    sprite.siteCollisionWith(tiledLayer_peng01, 左中) ||
                    sprite.siteCollisionWith(tiledLayer_peng01, 左下)) {
                sprite.setMirror(true);
                sprite.move(10, 0);

            }
            //右边碰撞
            if (sprite.siteCollisionWith(tiledLayer_peng01, 右上) ||
                    sprite.siteCollisionWith(tiledLayer_peng01, 右中) ||
                    sprite.siteCollisionWith(tiledLayer_peng01, 右下)) {
                sprite.setMirror(false);
                sprite.move(-10, 0);

            }

        }
    }


    /**
     * 精灵碰撞
     *
     * @param sprites0 主动碰撞的精灵数组
     * @param sprites1 被碰精灵数组
     */


    private void sptitesCollisionSprites(List<Sprite> sprites0, List<Sprite> sprites1) {
        if (sprites0 != null) {
            for (int i = 0; i < sprites0.size(); i++) {
                sptiteCollisionSprite(sprites0.get(i), sprites1);
            }
        }
    }


    /**
     * 精灵碰撞
     *
     * @param sprite0 主动碰撞的精灵
     * @param sprites 被碰精灵数组
     */

    private void sptiteCollisionSprite(Sprite sprite0, List<Sprite> sprites) {
        if (sprites != null) {
            for (int i = 0; i < sprites.size(); i++) {
                Sprite sprite1 = sprites.get(i);
                sptiteCollisionSprite(sprite0, sprite1);
            }
        }
    }

    /**
     * 精灵碰撞
     *
     * @param sprite0 主动碰撞的精灵
     * @param sprite1 被碰精灵
     */
    private void sptiteCollisionSprite(Sprite sprite0, Sprite sprite1) {
        //精灵可见且非死亡状态
        if (sprite0.isVisiable() && !sprite0.isDead() && sprite1.isVisiable() && !sprite1.isDead()) {
            //0底部碰撞1
            if (sprite0.siteCollisionWith(sprite1, 下)) {
                //乌龟特殊处理
                if (sprite0 instanceof Turtle) {
                    sprite0.setJumping(true);
                    sprite0.setSpeedY(-10);
                } else
                    //砖块上方有敌人 砖块被顶起
                    if (sprite0 instanceof Enemy && !(sprite0 instanceof Cannon) && sprite1 instanceof
                            Brick && sprite1.isJumping()) {
                        ((Enemy) sprite0).setOverturn(true);
                        sprite0.setSpeedY(-10);
                        sprite0.setDead(true);
                        updateScore(100);
                    } else
                        //砖块上方有道具 砖块被顶起
                        if (sprite0 instanceof ItemSprite && sprite1 instanceof CommonBrick && sprite1.isJumping()) {
                            sprite0.setSpeedY(-10);
                            sprite0.setJumping(true);

                        } else
                            //子弹与无敌星弹跳处理
                            if (sprite0 instanceof Bullet || sprite0 instanceof Star) {
                                sprite0.setSpeedY(-10);
                                sprite0.setJumping(true);
                            } else {
                                sprite0.setJumping(false);
                                //坐标修正
                                //取得脚部坐标
                                int footY = sprite0.getY() + sprite0.getHeight();
                                //取整
                                int newY = footY / tiledLayer_peng01.getHeight()
                                        * tiledLayer_peng01.getHeight()
                                        - sprite0.getHeight();
                                sprite0.setPosition(sprite0.getX(), newY);
                            }
            }
            if (sprite0.siteCollisionWith(sprite1, Site.上)) {
                //玛丽顶砖块
                if (sprite0 instanceof Mario && sprite0.isJumping()) {
                    if (sprite1 instanceof CommonBrick) {
                        sprite0.setSpeedY(Math.abs(sprite0.getSpeedY()));
                        if (!sprite1.isJumping()) {
                            if (mario.getStatus() != 0) {
                                ((CommonBrick) sprite1).setCanBroken(true);
                                sprite1.setSpeedY(-4);
                                sprite1.setJumping(true);
//                                mySoundPool.play(mySoundPool.getBrokenSound());
                            } else {
                                sprite1.setSpeedY(-4);
                                sprite1.setJumping(true);
//                                mySoundPool.play(mySoundPool.getCannotbreakSound());
                            }
                        }
                    } else if (sprite1 instanceof Brick) {
                        sprite0.setSpeedY(Math.abs(sprite0.getSpeedY()));
                        if (((Brick) sprite1).hasItem()) {
//                            mySoundPool.play(mySoundPool.getHitbrickSound());
                            sprite1.setSpeedY(-4);
                            sprite1.setJumping(true);
                        }
                    }
                } else {
                    if (sprite0 instanceof Bullet) {
                        sprite0.setVisiable(false);
                    } else {
                        sprite0.setSpeedY(Math.abs(sprite0.getSpeedY()));
                    }
                }

            }
            if (sprite0.siteCollisionWith(sprite1, 左)) {
                if (sprite0 instanceof Bullet) {
                    sprite0.setVisiable(false);
                } else {
                    sprite0.setMirror(true);
                    sprite0.move(4, 0);
                }
            }
            if (sprite0.siteCollisionWith(sprite1, 右)) {
                if (sprite0 instanceof Bullet) {
                    sprite0.setVisiable(false);
                } else {
                    sprite0.setMirror(false);
                    sprite0.move(-4, 0);
                }
            }
            //乌龟壳
        } else if (sprite0.isVisiable() && sprite0.isDead() && sprite1.isVisiable() &&
                !sprite1.isDead() && sprite0 instanceof Turtle && !(sprite1 instanceof Turtle)) {
            HiLog.info(LABEL_LOG, "sptiteCollisionSprite: 龟壳碰撞");
            //0底部碰撞1
            if (sprite0.siteCollisionWith(sprite1, 下)) {
                sprite0.setJumping(false);
                //坐标修正
                //取得脚部坐标
                int footY = sprite0.getY() + sprite0.getHeight();
                //取整
                int newY = footY / tiledLayer_peng01.getHeight()
                        * tiledLayer_peng01.getHeight()
                        - sprite0.getHeight();
                sprite0.setPosition(sprite0.getX(), newY);
            }
            if (sprite0.siteCollisionWith(sprite1, 左)) {
                sprite0.setMirror(true);
                sprite0.move(10, 0);
            }
            if (sprite0.siteCollisionWith(sprite1, 右)) {

                sprite0.setMirror(false);
                sprite0.move(-10, 0);

            }

        }
    }



    @Override
    public void addDrawTask(DrawTask task) {
        super.addDrawTask(task);
        task.onDraw(this,mCanvasForTaskOverContent);
    }

    @Override
    public boolean onEstimateSize(int i, int i1) {
        int width = Component.EstimateSpec.getSize(i);
        int height = Component.EstimateSpec.getSize(i1);
        setEstimatedSize(
                Component.EstimateSpec.getChildSizeWithMode(width, width, Component.EstimateSpec.PRECISE),
                Component.EstimateSpec.getChildSizeWithMode(height, height, Component.EstimateSpec.PRECISE)
        );
        return true;
    }

    @Override
    public void run() {
        isFirstRun = true;
        init();
        int i = 0;
        while (isRunning) {
            //游戏暂停
            if (!isPause()) {
                myLogic();
            }
            long startTime = System.currentTimeMillis();
            myDraw();
            long endTime = System.currentTimeMillis();
            long time = endTime - startTime;
            i++;
            System.out.println("================================new Thread()==================================" + i);
            if (time < 1000/35) {
                try {
                    Thread.sleep(1000/35 - time);
                    //刷新UI
                    TaskDispatcher uiTaskDispatcher = context.getUITaskDispatcher();
                    uiTaskDispatcher.asyncDispatch(() -> this.invalidate());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
